package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import org.summer.common.core.constant.Constants;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.constant.enums.DispatchOrderStatus;
import org.summer.lawyer.constant.enums.OrderStatus;
import org.summer.lawyer.domain.SpuOrderAssign;
import org.summer.lawyer.domain.bo.spuorderassign.SpuOrderAssignBo;
import org.summer.lawyer.domain.bo.spuorderassign.TakeCompletedOrderBo;
import org.summer.lawyer.domain.bo.spuorderassign.TakeOrderBo;
import org.summer.lawyer.domain.vo.lawfirm.LawFirmVo;
import org.summer.lawyer.domain.vo.spuorderassign.SpuOrderAssignVo;
import org.summer.lawyer.event.SpuOrderTimeoutEvent;
import org.summer.lawyer.mapper.SpuOrderAssignMapper;
import org.summer.lawyer.service.ILawFirmService;
import org.summer.lawyer.service.ISpuOrderAssignService;
import org.summer.lawyer.util.IdWorkerUtil;
import org.summer.lawyer.util.ParamsUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 派单Service业务层处理
 *
 * @author summer
 * @date 2024-06-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SpuOrderAssignServiceImpl extends ServiceImpl<SpuOrderAssignMapper, SpuOrderAssign> implements ISpuOrderAssignService , ApplicationEventPublisherAware {


    private final ILawFirmService lawFirmService;


    /**
     * 查询派单
     *
     * @param id 主键
     * @return 派单
     */
    @Override
    public SpuOrderAssignVo selectSpuOrderAssignVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public SpuOrderAssignVo selectSpuOrderAssignVoByTrackingNumber(String trackingNumber) {
        SpuOrderAssignBo bo = new SpuOrderAssignBo();
        bo.setTrackingNumber(trackingNumber);
        List<SpuOrderAssignVo> voList = selectSpuOrderAssignLawFirmList(bo);
        if (CollUtil.isNotEmpty(voList)) {
            return voList.getFirst();
        }
        return null;
    }

    @Override
    public SpuOrderAssignVo selectSpuOrderAssignVoBySpuOrderId(Long spuOrderId) {
        SpuOrderAssignBo bo = new SpuOrderAssignBo();
        bo.setOrderId(spuOrderId);
        bo.setActive("1");
        List<SpuOrderAssignVo> voList = selectSpuOrderAssignLawFirmList(bo);
        if (CollUtil.isNotEmpty(voList)) {
            return voList.getFirst();
        }
        return null;
    }

    /**
     * 分页查询派单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 派单分页列表
     */
    @Override
    public TableDataInfo<SpuOrderAssignVo> selectPageList(SpuOrderAssignBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = buildQueryWrapper(bo);
        Page<SpuOrderAssignVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<SpuOrderAssignVo> selectPageLawFirmList(SpuOrderAssignBo bo, PageQuery pageQuery) {
        QueryWrapper<SpuOrderAssignBo> lqw = buildLawFirmQueryWrapper(bo);
        Page<SpuOrderAssignVo> result = baseMapper.selectPageLawFirmList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private QueryWrapper<SpuOrderAssignBo> buildLawFirmQueryWrapper(SpuOrderAssignBo bo) {
        Map<String, Object> params = ParamsUtil.getParams(bo.getParams());
        if (StrUtil.isNotBlank(bo.getOrderStatus()) && "Processed".equals(bo.getOrderStatus())) {
            bo.setOrderStatus(OrderStatus.PROCESSING.getCode());
            bo.setProcessedStatus("1");
        }
        if (StrUtil.isNotBlank(bo.getOrderStatus()) && DispatchOrderStatus.RETURNED.getCode().equals(bo.getOrderStatus())) {
            bo.setOrderStatus(OrderStatus.PROCESSING.getCode());
            bo.setDispatchStatus(DispatchOrderStatus.RETURNED.getCode());
        }
        QueryWrapper<SpuOrderAssignBo> lqw = Wrappers.query();
        lqw.eq(bo.getId() != null, "soa.id", bo.getId());
        lqw.eq(bo.getOrderId() != null, "soa.order_id", bo.getOrderId());
        lqw.eq(bo.getLawFirmId() != null, "soa.law_firm_id", bo.getLawFirmId());
        lqw.eq("soa.del_flag", Constants.DEL_FLAG_NOT_DEL);
        lqw.eq("so.del_flag", Constants.DEL_FLAG_NOT_DEL);
        lqw.eq(StrUtil.isNotBlank(bo.getPayed()), "so.payed", bo.getPayed());
        lqw.eq(StrUtil.isNotBlank(bo.getActive()), "soa.active", bo.getActive());
        lqw.eq(StrUtil.isNotBlank(bo.getOrderStatus()), "so.order_status", bo.getOrderStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getProcessedStatus()), "so.processed_status", bo.getProcessedStatus());
        lqw.like(StrUtil.isNotBlank(bo.getOrderNumber()), "so.order_number", bo.getOrderNumber());
        lqw.like(StrUtil.isNotBlank(bo.getContactPhone()), "so.contact_phone", bo.getContactPhone());
        lqw.like(StrUtil.isNotBlank(bo.getServiceType()), "so.service_type", bo.getServiceType());
        lqw.like(StrUtil.isNotBlank(bo.getLawFirmName()),"soa.law_firm_name", bo.getLawFirmName());
        lqw.eq(StrUtil.isNotBlank(bo.getTrackingNumber()), "soa.tracking_number", bo.getTrackingNumber());
        lqw.eq(StrUtil.isNotBlank(bo.getDispatchStatus()),"soa.dispatch_status", bo.getDispatchStatus());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, "so.payment_time", params.get("beginTime"), params.get("endTime"));
        lqw.between(bo.getOrderBeginTime() != null && bo.getOrderEndTime() != null, "so.order_time", bo.getOrderBeginTime(), bo.getOrderEndTime());
        lqw.orderByDesc("soa.tracking_number");
        return lqw;
    }

    @Override
    public List<SpuOrderAssignVo> selectSpuOrderAssignLawFirmList(SpuOrderAssignBo bo) {
//        Map<String, Object> params = ParamsUtil.getParams(bo.getParams());
//        QueryWrapper<SpuOrderAssignBo> lqw = Wrappers.query();
//        lqw.eq(bo.getId() != null, "soa.id", bo.getId());
//        lqw.eq(bo.getOrderId() != null, "soa.order_id", bo.getOrderId());
//        lqw.eq(bo.getLawFirmId() != null, "soa.law_firm_id", bo.getLawFirmId());
//        lqw.eq("soa.del_flag", Constants.DEL_FLAG_NOT_DEL);
//        lqw.eq("so.del_flag", Constants.DEL_FLAG_NOT_DEL);
//        lqw.eq(StrUtil.isNotBlank(bo.getActive()), "soa.active", bo.getActive());
//        lqw.eq(StrUtil.isNotBlank(bo.getOrderStatus()), "so.order_status", bo.getOrderStatus());
//        lqw.eq(StrUtil.isNotBlank(bo.getProcessedStatus()), "so.processed_status", bo.getProcessedStatus());
//        lqw.eq(StrUtil.isNotBlank(bo.getOrderNumber()), "so.order_number", bo.getOrderNumber());
//        lqw.eq(StrUtil.isNotBlank(bo.getTrackingNumber()), "soa.tracking_number", bo.getTrackingNumber());
//        lqw.like(StrUtil.isNotBlank(bo.getContactPhone()), "so.contact_phone", bo.getContactPhone());
//        lqw.like(StrUtil.isNotBlank(bo.getServiceType()), "so.service_type", bo.getServiceType());
//        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, "so.payment_time", params.get("beginTime"), params.get("endTime"));
//        lqw.between(bo.getOrderBeginTime() != null && bo.getOrderEndTime() != null, "so.order_time", bo.getOrderBeginTime(), bo.getOrderEndTime());
//        lqw.orderByDesc("soa.tracking_number");
        QueryWrapper<SpuOrderAssignBo> lqw = buildLawFirmQueryWrapper(bo);
        return baseMapper.selectSpuOrderAssignLawFirmList(lqw);
    }

    /**
     * 查询符合条件的派单列表
     *
     * @param bo 查询条件
     * @return 派单列表
     */
    @Override
    public List<SpuOrderAssignVo> selectSpuOrderAssignVoList(SpuOrderAssignBo bo) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SpuOrderAssign> buildQueryWrapper(SpuOrderAssignBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, SpuOrderAssign::getOrderId, bo.getOrderId());
        lqw.eq(StrUtil.isNotBlank(bo.getTrackingNumber()), SpuOrderAssign::getTrackingNumber, bo.getTrackingNumber());
        lqw.eq(bo.getDispatchTime() != null, SpuOrderAssign::getDispatchTime, bo.getDispatchTime());
        lqw.eq(bo.getDispatchUserId() != null, SpuOrderAssign::getDispatchUserId, bo.getDispatchUserId());
        lqw.like(StrUtil.isNotBlank(bo.getDispatchName()), SpuOrderAssign::getDispatchName, bo.getDispatchName());
        lqw.eq(StrUtil.isNotBlank(bo.getDispatchStatus()), SpuOrderAssign::getDispatchStatus, bo.getDispatchStatus());
        lqw.eq(bo.getWxUserId() != null, SpuOrderAssign::getWxUserId, bo.getWxUserId());
        lqw.eq(bo.getLawyerId() != null, SpuOrderAssign::getLawyerId, bo.getLawyerId());
        lqw.like(StrUtil.isNotBlank(bo.getLawyerName()), SpuOrderAssign::getLawyerName, bo.getLawyerName());
        lqw.eq(bo.getLawFirmId() != null, SpuOrderAssign::getLawFirmId, bo.getLawFirmId());
        lqw.like(StrUtil.isNotBlank(bo.getLawFirmName()), SpuOrderAssign::getLawFirmName, bo.getLawFirmName());
        lqw.eq(StrUtil.isNotBlank(bo.getLawyerAvatar()), SpuOrderAssign::getLawyerAvatar, bo.getLawyerAvatar());
        lqw.eq(bo.getHandleTime() != null, SpuOrderAssign::getHandleTime, bo.getHandleTime());
        lqw.eq(bo.getReturnedTime() != null, SpuOrderAssign::getReturnedTime, bo.getReturnedTime());
        lqw.eq(StrUtil.isNotBlank(bo.getReturnedDesc()), SpuOrderAssign::getReturnedDesc, bo.getReturnedDesc());
        lqw.eq(bo.getCompletionTime() != null, SpuOrderAssign::getCompletionTime, bo.getCompletionTime());
        lqw.eq(bo.getSpuId() != null, SpuOrderAssign::getSpuId, bo.getSpuId());
        lqw.like(StrUtil.isNotBlank(bo.getSpuName()), SpuOrderAssign::getSpuName, bo.getSpuName());
        lqw.eq(bo.getCategoryId() != null, SpuOrderAssign::getCategoryId, bo.getCategoryId());
        lqw.like(StrUtil.isNotBlank(bo.getCategoryName()), SpuOrderAssign::getCategoryName, bo.getCategoryName());
        lqw.eq(StrUtil.isNotBlank(bo.getSpuImage()), SpuOrderAssign::getSpuImage, bo.getSpuImage());
        lqw.eq(bo.getSpuPrice() != null, SpuOrderAssign::getSpuPrice, bo.getSpuPrice());
        lqw.eq(bo.getSpuCount() != null, SpuOrderAssign::getSpuCount, bo.getSpuCount());
        lqw.eq(bo.getSpuTotalAmount() != null, SpuOrderAssign::getSpuTotalAmount, bo.getSpuTotalAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getSpuFile()), SpuOrderAssign::getSpuFile, bo.getSpuFile());
        return lqw;
    }

    /**
     * 新增派单
     *
     * @param bo 派单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SpuOrderAssignBo bo) {
        SpuOrderAssign add = MapstructUtils.convert(bo, SpuOrderAssign.class);
        validEntityBeforeSave(add);
        add.setTrackingNumber(IdWorkerUtil.nextIdStr());
        add.setDispatchStatus(DispatchOrderStatus.PENDING.getCode());
        add.setOvertime("0");
        add.setActive("1");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改派单
     *
     * @param bo 派单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SpuOrderAssignBo bo) {
        SpuOrderAssign update = MapstructUtils.convert(bo, SpuOrderAssign.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SpuOrderAssign entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除派单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void takeOrder(TakeOrderBo bo) {
        SpuOrderAssignVo vo = selectSpuOrderAssignVoByTrackingNumber(bo.getTrackingNumber());
        if (SystemConstants.INACTIVE.equals(vo.getActive())) {
            throw new ServiceException("当前订单已经关闭");
        }
        if (DispatchOrderStatus.PROCESSING.getCode().equals(vo.getDispatchStatus())) {
            SpuOrderAssign spuOrderAssign = new SpuOrderAssign();
            spuOrderAssign.setId(vo.getId());
            spuOrderAssign.setDispatchStatus(DispatchOrderStatus.CONFIRMED.getCode());
            spuOrderAssign.setHandleTime(DateUtil.date());
            LambdaUpdateWrapper<SpuOrderAssign> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(SpuOrderAssign::getId, vo.getId());
            baseMapper.update(spuOrderAssign, lambdaUpdateWrapper);
        }
    }

    @Override
    public void takeCompletedOrder(TakeCompletedOrderBo bo) {
        SpuOrderAssignVo vo = selectSpuOrderAssignVoByTrackingNumber(bo.getTrackingNumber());
        if (SystemConstants.INACTIVE.equals(vo.getActive())) {
            throw new ServiceException("当前订单已经关闭");
        }
    }

    /**
     * 律所全部订单数量
     */
    @Override
    public Long orderCountByLawFirmId(Long lawFirmId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getLawFirmId, lawFirmId);
        return baseMapper.selectCount(lqw);
    }

    /**
     * 律所全部完成订单数量
     */
    @Override
    public Long completedOrderCountByLawFirmId(Long lawFirmId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getLawFirmId, lawFirmId);
        lqw.eq(SpuOrderAssign::getDispatchStatus, DispatchOrderStatus.COMPLETED);
        return baseMapper.selectCount(lqw);
    }

    /**
     * 律所全部待完成订单数量
     */
    @Override
    public Long confirmedOrderCountByLawFirmId(Long lawFirmId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getLawFirmId, lawFirmId);
        lqw.eq(SpuOrderAssign::getDispatchStatus, DispatchOrderStatus.CONFIRMED);
        return baseMapper.selectCount(lqw);
    }

    /**
     * 律所全部退回（超时或已过期）订单数量
     */
    @Override
    public Long returnedOrderCountByLawFirmId(Long lawFirmId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getLawFirmId, lawFirmId);
        lqw.eq(SpuOrderAssign::getDispatchStatus, DispatchOrderStatus.RETURNED);
        return baseMapper.selectCount(lqw);
    }

    @Override
    public Long reviewOrderCountByLawFirmId(Long lawFirmId) {
        return baseMapper.reviewOrderCountByLawFirmId(lawFirmId);
    }

    /**
     * 派单
     */
    @Override
    public void assigningOrder(SpuOrderAssignBo bo) {
        //获取订单信息
        SpuOrderAssignVo orderAssignVo = selectSpuOrderAssignVoByTrackingNumber(bo.getTrackingNumber());
        SpuOrderAssign spuOrderAssign = new SpuOrderAssign();
        spuOrderAssign.setDispatchStatus(DispatchOrderStatus.PROCESSING.getCode());
        spuOrderAssign.setDispatchTime(DateUtil.date());
        if (LoginHelper.getLoginUser() != null) {
            spuOrderAssign.setDispatchUserId(LoginHelper.getLoginUser().getUserId());
            spuOrderAssign.setDispatchName(LoginHelper.getLoginUser().getNickname());
        }
        LawFirmVo lawFirmVo = lawFirmService.selectLawFirmVoById(bo.getLawFirmId());
        spuOrderAssign.setWxUserId(lawFirmVo.getAdminUserId());
        spuOrderAssign.setLawFirmId(lawFirmVo.getLawFirmId());
        spuOrderAssign.setLawFirmName(lawFirmVo.getLawFirmName());
        spuOrderAssign.setLawyerAvatar(lawFirmVo.getLawFirmLogo());
        spuOrderAssign.setDeadline(DateUtil.offsetMinute(DateUtil.date(), 10));
        spuOrderAssign.setId(orderAssignVo.getId());
        LambdaUpdateWrapper<SpuOrderAssign> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SpuOrderAssign::getId, orderAssignVo.getId());
        baseMapper.update(spuOrderAssign, updateWrapper);
    }

    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public void cancelAssignOrder(Long id) {
        SpuOrderAssignVo spuOrderAssignVo = selectSpuOrderAssignVoById(id);
        //防止重复处理，加个判断
        if (DispatchOrderStatus.PROCESSING.getCode().equals(spuOrderAssignVo.getDispatchStatus())) {
            SpuOrderAssignBo bo = new SpuOrderAssignBo();
            bo.setId(spuOrderAssignVo.getId());
            bo.setTrackingNumber(spuOrderAssignVo.getTrackingNumber());
            bo.setReturnedDesc("超时未接单，系统自动撤回");
            this.revokeSpuOrder(bo);
        }
    }

    /**
     * 撤销订单
     */
    @Override
    public void revokeSpuOrder(SpuOrderAssignBo bo) {
        SpuOrderAssignVo spuOrderAssignVo = selectSpuOrderAssignVoByTrackingNumber(bo.getTrackingNumber());
        if(DispatchOrderStatus.RETURNED.getCode().equals(spuOrderAssignVo.getDispatchStatus())){
            throw new ServiceException("订单已撤回，请勿重复处理");
        }
        if(DispatchOrderStatus.COMPLETED.getCode().equals(spuOrderAssignVo.getDispatchStatus())){
            throw new ServiceException("订单已完成，不支持撤回");
        }
        SpuOrderAssign spuOrderAssign = new SpuOrderAssign();
        spuOrderAssign.setId(spuOrderAssignVo.getId());
        spuOrderAssign.setDispatchStatus(DispatchOrderStatus.RETURNED.getCode());
        spuOrderAssign.setActive(SystemConstants.INACTIVE);
        spuOrderAssign.setReturnedTime(DateUtil.date());
        spuOrderAssign.setReturnedDesc(bo.getReturnedDesc());
        LambdaUpdateWrapper<SpuOrderAssign> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SpuOrderAssign::getId, spuOrderAssignVo.getId());
        baseMapper.update(spuOrderAssign, updateWrapper);
        //创建新分派订单
        SpuOrderAssignBo newSpuOrderAssignBo = new SpuOrderAssignBo();
        BeanUtils.copyProperties(spuOrderAssignVo, newSpuOrderAssignBo);
        newSpuOrderAssignBo.setId(null);
        newSpuOrderAssignBo.setDispatchUserId(null);
        newSpuOrderAssignBo.setDispatchName(null);
        newSpuOrderAssignBo.setDispatchTime(null);
        newSpuOrderAssignBo.setWxUserId(null);
        newSpuOrderAssignBo.setLawyerId(null);
        newSpuOrderAssignBo.setLawyerName(null);
        newSpuOrderAssignBo.setLawFirmId(null);
        newSpuOrderAssignBo.setLawFirmName(null);
        newSpuOrderAssignBo.setLawyerAvatar(null);
        newSpuOrderAssignBo.setHandleTime(null);
        newSpuOrderAssignBo.setDeadline(null);
        insertByBo(newSpuOrderAssignBo);


    }

    @Override
    public void completedAssigningOrder(SpuOrderAssignBo bo) {
        SpuOrderAssign spuOrderAssign = new SpuOrderAssign();
        spuOrderAssign.setDispatchStatus(DispatchOrderStatus.FINISHED.getCode());
        spuOrderAssign.setFinishedTime(bo.getFinishedTime());
        spuOrderAssign.setId(bo.getId());
        LambdaUpdateWrapper<SpuOrderAssign> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SpuOrderAssign::getId, bo.getId());
        baseMapper.update(spuOrderAssign, updateWrapper);
    }

    @Override
    public void processedOrder(SpuOrderAssignBo bo) {
        SpuOrderAssign spuOrderAssign = new SpuOrderAssign();
        spuOrderAssign.setId(bo.getId());
        spuOrderAssign.setDispatchStatus(DispatchOrderStatus.COMPLETED.getCode());
        spuOrderAssign.setCompletionTime(DateUtil.date());
        LambdaUpdateWrapper<SpuOrderAssign> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SpuOrderAssign::getId, bo.getId());
        baseMapper.update(spuOrderAssign, updateWrapper);
    }

    @Override
    public List<SpuOrderAssignVo> selectTimeoutSpuOrderAssignList(SpuOrderAssignBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getDispatchStatus, DispatchOrderStatus.PROCESSING.getCode());
        lqw.le(SpuOrderAssign::getDeadline, params.get("now"));
        return baseMapper.selectVoList(lqw);
    }

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Override
    public Long selectSpuOrderAssignCount(Long orderId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getOrderId, orderId);
        return baseMapper.selectCount(lqw);
    }

    @Override
    public SpuOrderAssignVo selectLastInactiveSpuOrderAssign(Long orderId) {
        LambdaQueryWrapper<SpuOrderAssign> lqw = Wrappers.lambdaQuery();
        lqw.eq(SpuOrderAssign::getOrderId, orderId);
        lqw.eq(SpuOrderAssign::getActive, "0");
        lqw.orderByDesc(SpuOrderAssign::getId);
        return baseMapper.selectVoOne(lqw, false);
    }
}
